<template>
  <el-text><h3>修改密码</h3></el-text>
  <el-divider />
  <el-form ref="form" :model="user">
    <el-form-item>
      <el-input v-model="user.password" show-password placeholder="请输入新密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input
        v-model="user.repassword" show-password 
        placeholder="请输入确认新密码"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="btn_repassword">确认修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import http from '../axios'
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { useRouter } from "vue-router";

const user = ref({});

const router = useRouter();

const btn_repassword = () => {
  if (!user.value.password || !user.value.repassword) {
    ElMessage.warning("请输入密码");
    return;
  }
  if (user.value.password.trim().length == 0){
        ElMessage.warning('密码不能为空格！');
        return;
  }
  console.log(user.value.password);
  console.log(user.value.repassword);
  if (user.value.password !== user.value.repassword) {
    ElMessage.warning("请输入相同的密码");
    return;
  }
  http.put("/user/update", user.value).then((resp) => {
    if (resp.data.code != "200") {
      ElMessage.error(resp.data.message);
      return;
    }
    ElMessage({
      message: resp.data.message,
      type: "success",
      duration: 2000,
      onClose: () => {
        localStorage.clear();
        router.replace("/login");
      },
    });
  });
};
</script>

<style></style>
